package com.cn.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.cn.entity.Department;
import com.cn.entity.TiJianxiangmu;


public class TiJianXiangmuDao {
	

	private static SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	Session session;

	
	
	public List<TiJianxiangmu> scanAll(){
		session=sessionFactory.openSession();
		String hql="from TiJianxiangmu";
		Query query=session.createQuery(hql);
		List<TiJianxiangmu> list=query.list();
	   
	     return list;
		
		
	}
	public TiJianxiangmu scanOne(int xiangmuid){
		TiJianxiangmu xiangmu=null;
		session=sessionFactory.openSession();
		String hql="from TiJianxiangmu where xiangmuid=?";
		Query query=session.createQuery(hql);
		query.setInteger(0, xiangmuid);
		List<TiJianxiangmu> list=query.list();
	   
		if (list.size()>0){xiangmu=list.get(0);}
		
	     return xiangmu;
		
		
	}

	public void add(TiJianxiangmu xiangmu){
		session=sessionFactory.openSession();
		Transaction tr=session.beginTransaction();
		
		session.save(xiangmu);
		tr.commit();
		session.close();
		
		
		
	}
	
	public void update(TiJianxiangmu xiangmu){
		session=sessionFactory.openSession();
		Transaction tr=session.beginTransaction();
		
		session.update(xiangmu);
		tr.commit();
		session.close();
		
		
		
	}
	
	public void delete(TiJianxiangmu xiangmu){
		
		Transaction tr=session.beginTransaction();
		
		session.delete(xiangmu);
		tr.commit();
		session.close();
		
		
		
	}
	
	public TiJianxiangmu scanOne(String xiangmuming){
		TiJianxiangmu xiangmu=null;
		session=sessionFactory.openSession();
		String hql="from TiJianxiangmu where xiangmuming=?";
		Query query=session.createQuery(hql);
		query.setString(0, xiangmuming);
		List<TiJianxiangmu> list=query.list();
	   
		if (list.size()>0){xiangmu=list.get(0);}
		
	     return xiangmu;
		
		
	}
}
